<?php
require __DIR__ . '/DB.php';
require __DIR__ . '/tools.php';//数据处理类
/**
 * 链接数据库
 */
$db = new DB();
$db->__setup([
    'dsn'=>'mysql:dbname=silent;host=den1.mysql4.gear.host',
    'username'=>'silent',
    'password'=>'Hj0ky28?-7TF',
    'charset'=>'utf8'
]);

//实时查询有没有人往这个id发送消息
function selectNewData($id){
    global $db;
    $users = $db->fetchAll("SELECT text.id id,`name`,time,face,content FROM `text` 
    LEFT JOIN `user` ON text.user_id = user.id WHERE sh_user_id = $id AND state = 1 ORDER BY time ASC;");

    if($users==""&&$users==null&&empty($users)){
        return false;
    }else{
       return getArr("1",$users);
    }
}

/**
 * 消息被收到后进行状态变更
 * @param id 消息id
 */
function sure($id){
    global $db;
    $parameters  = ["state"=>"0"];
    $condition = ["id"=>$id];
    $db->update("text", $parameters, $condition);
}
/**
 * 用户发送消息
 * @param $fa_id  发送者
 * @param $shou_id    接受者
 * @param $conten  内容是
 *
 */
function creat($fa_id,$shou_id,$conten){
    global $db;
    $time = time();
    $parameters  = ["user_id"=>$fa_id,"sh_user_id"=>$shou_id,"content"=>$conten,"time"=>$time,"state"=>"1"];
    $res = $db->insert("text", $parameters);

    if($res > 0){
        echo  upTime($time);
    }
}